package com.dushibao.test;

import com.dushibao.GenerHtmlForVelocity;
import com.dushibao.utils.StringUtils;
import com.dushibao.utils.TypeUtils;
import com.dushibao.vo.ClazzVO;
import com.dushibao.vo.FieldVO;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.junit.Test;

import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class Generator {

    String DEF_ENCODING = "utf-8";
    String database = "dushibao";
    String url = "jdbc:mysql://127.0.0.1:3306/"+database+"?serverTimezone=GMT&useInformationSchema=true";
    String user = "root";
    String password = "root";
    String driver = "com.mysql.cj.jdbc.Driver";

    String author = "zqd";
    String basePackgeName = "com.dushibao.admin";
    String baseFolderName = "com/dushibao/admin/";

    String clzzName = "AdminMenu";
    String simpleClzzName = "adminMenu";
    String tableName = "t_admin_menu";

    String copyright = "Copyright (c) 2021";
    String webSite = "www.dushibao.com";
    String version = "dushibao V10.0";
    String applicationName = "AdminApplication";



    @Test
    public void dbInfo() throws Exception {
        ClazzVO clazzVO = new ClazzVO();
        clazzVO.setAuthor(author);
        clazzVO.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        clazzVO.setTableName(tableName);
        clazzVO.setClazzName(clzzName);
        clazzVO.setCopyright(copyright);
        clazzVO.setWebSite(webSite);
        clazzVO.setVersion(version);
        clazzVO.setApplicationName(applicationName);

        List<FieldVO> fieldVOS = new ArrayList<>();

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);

        DatabaseMetaData metaData = conn.getMetaData();

        ResultSet resultSet = metaData.getTables(database,tableName,tableName,new String[]{"TABLE"});

        while(resultSet.next()){
            String TABLE_CAT = resultSet.getString("TABLE_CAT");
            String TABLE_SCHEM = resultSet.getString("TABLE_SCHEM");
            String TABLE_NAME = resultSet.getString("TABLE_NAME");
            String TABLE_TYPE = resultSet.getString("TABLE_TYPE");
            String REMARKS = resultSet.getString("REMARKS");

            System.out.println("TABLE_CAT = " + TABLE_CAT);
            System.out.println("TABLE_SCHEM = " + TABLE_SCHEM);
            System.out.println("TABLE_NAME = " + TABLE_NAME);
            System.out.println("TABLE_TYPE = " + TABLE_TYPE);
            System.out.println("REMARKS = " + REMARKS);

            clazzVO.setRemarks(REMARKS);

        }

        ResultSet rs = metaData.getColumns(database, tableName,tableName, "%");

        while(rs.next()){
            String COLUMN_NAME = rs.getString("COLUMN_NAME");
            String DATA_TYPE = rs.getString("DATA_TYPE");
            String TYPE_NAME = rs.getString("TYPE_NAME");
            String REMARKS = rs.getString("REMARKS");
            String IS_AUTOINCREMENT = rs.getString("IS_AUTOINCREMENT");

            System.out.println("COLUMN_NAME = " + COLUMN_NAME);
            System.out.println("DATA_TYPE = " + DATA_TYPE);
            System.out.println("TYPE_NAME = " + TYPE_NAME);
            System.out.println("JAVA_TYPE = " + TypeUtils.getJavaTypeByMysqlType(TYPE_NAME));
            System.out.println("REMARKS = " + REMARKS);
            System.out.println("IS_AUTOINCREMENT = " + IS_AUTOINCREMENT);

            FieldVO fieldVO = new FieldVO();
            fieldVO.setFieldName(StringUtils.columnNameToJavaName(COLUMN_NAME));
            fieldVO.setFieldType(TypeUtils.getJavaTypeByMysqlType(TYPE_NAME));
            fieldVO.setAuto(IS_AUTOINCREMENT);
            fieldVO.setColumnName(COLUMN_NAME);
            fieldVO.setColumnType(TYPE_NAME);
            fieldVO.setComments(REMARKS);

            fieldVOS.add(fieldVO);

            //private Integer id;
            System.out.println("-------------------------------");
        }

        VelocityContext context = new VelocityContext();
        context.put("basePackgeName",basePackgeName);
        context.put("baseFolderName",baseFolderName);
        context.put("clazzVO",clazzVO);
        context.put("fieldVOS",fieldVOS);

        context.put("notice1","商品列表");
        context.put("notice2","商品分页");
        context.put("notice3","商品新增、删除");

        createFile("Model.vm",baseFolderName+"model",clzzName + ".java",context);
        createFile("Mapper.vm",baseFolderName+"mapper",clzzName + "Mapper.java",context);
        createFile("IService.vm",baseFolderName+"service","I"+clzzName + "Service.java",context);
        createFile("ServiceImpl.vm",baseFolderName+"service/impl",clzzName + "ServiceImpl.java",context);
        createFile("Controller.vm",baseFolderName+"controller",clzzName + "Controller.java",context);
        createFile("ModelPageRequest.vm",baseFolderName+"vo/request",clzzName + "ModelPageRequest.java",context);
        createFile("TestServiceDemo.vm","test/java/"+baseFolderName,clzzName + "ServiceDemo.java",context);
        createFile("list.html","/"+simpleClzzName,"list.html",context);
        createFile("add.html","/"+simpleClzzName,"add.html",context);
    }

    public void createFile(String fileName,String folder,String destName,VelocityContext context) throws Exception {
        Properties p = new Properties();
        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
        p.setProperty(Velocity.ENCODING_DEFAULT, DEF_ENCODING);
        p.setProperty(Velocity.INPUT_ENCODING, DEF_ENCODING);
        p.setProperty(Velocity.OUTPUT_ENCODING, DEF_ENCODING);
        Velocity.init(p);

        Template template = Velocity.getTemplate(GenerHtmlForVelocity.class.getClassLoader().getResource(fileName).getFile());

        File file = new File("src/main/resources/"+folder);
        file.mkdirs();

        FileOutputStream fos = new FileOutputStream("src/main/resources/"+folder+"/"+destName);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, DEF_ENCODING));

        template.merge(context, writer);
        writer.close();
    }

    @Test
    public void testCreateFile() throws Exception {
        Properties p = new Properties();
        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
        p.setProperty(Velocity.ENCODING_DEFAULT, DEF_ENCODING);
        p.setProperty(Velocity.INPUT_ENCODING, DEF_ENCODING);
        p.setProperty(Velocity.OUTPUT_ENCODING, DEF_ENCODING);
        Velocity.init(p);

        Template template = Velocity.getTemplate(GenerHtmlForVelocity.class.getClassLoader().getResource("myblog.html").getFile());

        VelocityContext context = new VelocityContext();

        List<String> blog_list = new ArrayList<String>();
        blog_list.add("wanghjtheory");
        blog_list.add("计算机组成原理");
        blog_list.add("JDK");
        blog_list.add("Oracle");
        blog_list.add("Tomcat");
        blog_list.add("Log4j");
        blog_list.add("FastJson");
        blog_list.add("Json");
        blog_list.add("反射");
        blog_list.add("SSI");
        blog_list.add("Velocity");
        blog_list.add("论文");
        blog_list.add("os X");
        blog_list.add("Js");
        blog_list.add("FreeMarker");
        blog_list.add("JSP");

        context.put("name", "wanghjbuf");
        context.put("blog_addr", "http://blog.csdn.net/wanghjbuf");
        context.put("blog_list", blog_list);
        context.put("last_modif", new Timestamp(System.currentTimeMillis()));

        File file = new File("src/main/resources/html");
        file.mkdirs();

        FileOutputStream fos = new FileOutputStream("src/main/resources/html/myblog.html");
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, DEF_ENCODING));

        template.merge(context, writer);
        writer.close();


    }


}
